<DynamicPageBanner title="{intl.followRequests}" icon="#fa-user-plus" />
{#if $isUserLoggedIn }
  <AccountsListPage {accountsFetcher} {accountActions} />
{/if}
<script>
  import AccountsListPage from '../_components/AccountsListPage.html'
  import { store } from '../_store/store.js'
  import { getFollowRequests } from '../_api/followRequests.js'
  import DynamicPageBanner from '../_components/DynamicPageBanner.html'
  import { setFollowRequestApprovedOrRejected } from '../_actions/requests.js'
  import { database } from '../_database/database.js'

  // sneakily update the follow reqs count in the cache, since we just fetched it
  function updateFollowReqsCount ($currentInstance, followReqs) {
    /* no await */ database.setFollowRequestCount($currentInstance, followReqs.length)
    const { followRequestCounts } = store.get()
    followRequestCounts[$currentInstance] = followReqs.length
    store.set({ followRequestCounts })
  }

  export default {
    data: () => ({
      accountActions: [
        {
          icon: '#fa-check',
          label: 'intl.approve',
          onclick: (accountId) => setFollowRequestApprovedOrRejected(accountId, true, true)
        },
        {
          icon: '#fa-times',
          label: 'intl.reject',
          onclick: (accountId) => setFollowRequestApprovedOrRejected(accountId, false, true)
        }
      ]
    }),
    computed: {
      accountsFetcher: ({ $currentInstance, $accessToken }) => async () => {
        const followReqs = await getFollowRequests($currentInstance, $accessToken)
        updateFollowReqsCount($currentInstance, followReqs)
        return followReqs
      }
    },
    store: () => store,
    components: {
      AccountsListPage,
      DynamicPageBanner
    }
  }
</script>
